package com.kun.config;

import cn.hutool.core.map.MapUtil;
import com.kun.interceptor.LoginUserInterceptor;
import com.kun.resolver.TokenArgumentResolver;
import com.kun.util.SpringContextHolder;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;
import java.util.Map;

/**
 * @author kun.li
 */
public class DefaultWebMvcConfig implements WebMvcConfigurer {


    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new TokenArgumentResolver());
        argumentResolversCustomizer(resolvers);
    }

    /**
     * 参数解析器自定义扩展
     */
    protected void argumentResolversCustomizer(List<HandlerMethodArgumentResolver> resolvers) {
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        Map<String, LoginUserInterceptor> beansOfType = SpringContextHolder.getApplicationContext().getBeansOfType(LoginUserInterceptor.class);
        if (MapUtil.isNotEmpty(beansOfType)) {
            registry.addInterceptor(beansOfType.get(LoginUserInterceptor.class.getName()));
        }
        handlerInterceptorRegistryCustomizer(registry);
    }

    /**
     * 拦截器自定义扩展
     */
    protected void handlerInterceptorRegistryCustomizer(InterceptorRegistry registry) {

    }
}
